<?php
/**
 * Created by PhpStorm.
 * FileName: LayeringBehavior.php
 * User: Administrator
 * Date: 2017/12/20
 * Time: 11:17
 */

namespace Admin\Behavior;

use Think\Behavior;

class LayeringBehavior extends Behavior {

    public function run(&$params) {
        $this->checkLayering();
    }

    /**
     * 检查当前分级控制器模块是否启用
     * @return bool
     */
    private function checkLayering(){
        $controller_level = C('CONTROLLER_LEVEL');
        if ($controller_level == 1) {
            return false;
        }
        $curController = explode('/',CONTROLLER_NAME);
        if (count($curController) < $controller_level) {
            return false;
        }
        $enabledController = C('ENABLED_CONTROLLER');
        if ($enabledController && !in_array(array_shift($curController),$enabledController)) {
            //根据配置，跳转到默认首页
            $defaultIndex = sprintf('%s/%s/%s',C('DEFAULT_MODULE'),C('DEFAULT_CONTROLLER'),C('DEFAULT_ACTION'));
            $str = '<div style="text-align: center;margin-top:150px;">该模块暂未启用，3秒后将为您跳转到系统首页！</div>';
            redirect(U($defaultIndex),3,$str);
        }
        return false;
    }
}